<?php
namespace Home\Model;
use Think\Model;
class ArticleModel extends Model {
	public $table='zjy_article';
	public $page; //当前页
	public $perpage; //每页显示
	public $article_id; //ID
	public $ac_id; //分类ID
	public $post; //提交的信息 数组
	public $ids; //IDs 主要用于删除
	public $sorts; //IDs 主要用于删除
	public $keyword; //关键词
	public $system_cat_list; //关键词
	public $ac_ids;
	public $limit_num;  //限制数

	//获取信息
	public function get_article_info()
	{
		$article_id=intval($this->article_id);
		//echo $article_id;exit;
		$Model=M("Article");
		$ArticleClass=M("Article_class");
		$info=$Model->where('article_id='.$article_id)->find();
		$info['ac_name']=$ArticleClass->where('ac_id='.$info['ac_id'])->getField('ac_name');
		//print_r($info);exit;
		return $info;
	}
	//获得上一篇文章信息
	public function get_last_article()
	{
		$article_id=intval($this->article_id);
		$ac_id=intval($this->ac_id);
		$Model=new Model();
		$query=$Model->query("SELECT article_id,article_title from ".$this->table." WHERE article_id<$article_id AND `ac_id`=$ac_id ORDER by `article_id` desc limit 1");
		return $query[0];
	}

	//获得下一篇文章信息
	public function get_next_article()
	{
		$article_id=intval($this->article_id);
		$ac_id=intval($this->ac_id);
		$Model=new Model();
		$query=$Model->query("SELECT article_id,article_title from ".$this->table." WHERE article_id>$article_id AND `ac_id`=$ac_id ORDER by `article_id` asc limit 1");
		return $query[0];
	}

	//获得文章列表 $page当前页 $perpage每页显示
	public function get_article_list()
	{
		$page=$this->page;
		$perpage=$this->perpage;
		//搜索
		$where='where article_type=1 ';
		/*if($this->keyword){
			$where.="and `article_title` LIKE '%".$this->keyword."%'";
		}*/
		if($this->ac_id){
			$where.="and c.ac_id=".$this->ac_id;
		}
		$start=($page-1)*$perpage;
		$Model=new Model();
		$list=$Model->query("select * from `zjy_article` a left join `zjy_article_class` c on a.ac_id=c.ac_id ".$where." order by a.article_sort asc,a.article_id desc limit $start,$perpage");
		return $list;
	}
	//获得文章总数
	public function get_article_total()
	{
		//搜索
		$where='where article_type=1 ';
		/*if($this->keyword){
			$where.="and `article_title` LIKE '%".$this->keyword."%'";
		}*/
		if($this->ac_id){
			$where.="and ac_id=".$this->ac_id;
		}
		$Model=new Model();
		$query=$Model->query("select count(article_id) as total from `zjy_article` ".$where." ");
		return $query[0]['total'];
	}
	//获得首页文章列表 $limit_num 限制数 $ac_id分类ID
	public function get_index_article_list()
	{
		$limit_num=$this->limit_num;
		$ac_id=$this->ac_id;
		$field='article_id,article_title,article_time';
		$where='article_type=1';
		$limit='0,10';
		//where
		if($ac_id>0){
			$where.=" and ac_id=".$ac_id;
		}
		//limit
		if($limit_num>0){
			$limit="0,".$limit_num;
		}
		$Model=M('Article');
		$list=$Model->field($field)->where($where)->order('article_sort asc,article_id desc')->limit($limit)->select();
		$list[0]['article_content']=$Model->where('article_id='.$list[0]['article_id'])->getField('article_content');
		//print_r($list);exit;
		//$list=$Model->query("select * from `zjy_article` a left join `zjy_article_class` c on a.ac_id=c.ac_id ".$where." order by a.article_sort asc,a.article_id desc limit $limit");
		return $list;
	}

	/*
	//提交文章
	public function article_add_post()
	{
		$Model=M("Article");
		$data=$this->post;
		if($Model->add($data)){
			return true;
		}else{
			return false;
		}
	}
	//编辑提交
	public function article_edit_post()
	{
		$Model=M("Article");
		$condition['article_id'] = $this->post['article_id'];
		$data=$this->post;
		if($Model->where($condition)->save($data)){
			return true;
		}else{
			return false;
		}
	}
	//删除
	public function delete_article()
	{
		$ids=$this->ids;
		$Model=M("Article");
		foreach($ids as $v){
			$condition['article_id']=$v;
			if(!$Model->where($condition)->delete()){
				return false;
			}
		}
		return true;
	}
	//批量更新排序
	public function update_article_sort()
	{
		$ids=$this->ids;
		$sorts=$this->sorts;
		$Model=M("Article");
		foreach($ids as $k=>$v){
			$data['article_sort']=$sorts[$k];
			$condition['article_id']=$v;
			$result=$Model->where($condition)->save($data);
			if($result === false) {
				return false;
			}
		}
		return true;
	}
	//系统文章部分
	//获得文章列表 $page当前页 $perpage每页显示
	public function get_system_article_list()
	{
		$page=$this->page;
		$perpage=$this->perpage;
		//搜索
		$where='where article_type=2 ';
		if($this->keyword){
			$where.="and `article_title` LIKE '%".$this->keyword."%'";
		}
		if($this->ac_id){
			$ac_ids=$this->get_sub_ac_id($this->ac_id);
			$ac_ids=mb_strcut($ac_ids,1); //裁掉第一个，
			$where.="and c.ac_id in(".$ac_ids.")";
		}
		$start=($page-1)*$perpage;
		$Model=new Model();
		$list=$Model->query("select * from `zjy_article` a left join `zjy_article_class` c on a.ac_id=c.ac_id ".$where." order by a.article_sort asc,a.article_id desc limit $start,$perpage");
		return $list;
	}
	//取分类下所有子分类
	private function get_sub_ac_id($ac_id){
		$ac_id=intval($ac_id);
		$this->ac_ids.=','.$ac_id;
		$Model=M("ArticleClass");
		$list=$Model->query("select `ac_id` from `zjy_article_class` where `ac_parent_id`=".$ac_id." order by ac_sort asc");
		if(is_array($list)){
			foreach($list as $k=>$v){
				$this->get_sub_ac_id($v['ac_id']);
			}
		}
		return $this->ac_ids;
	}
	//获得文章总数
	public function get_system_article_total()
	{
		//搜索
		$where='where article_type=2 ';
		if($this->keyword){
			$where.="and `article_title` LIKE '%".$this->keyword."%'";
		}
		if($this->ac_id){
			$ac_ids=$this->get_sub_ac_id($this->ac_id);
			$ac_ids=mb_strcut($ac_ids,1); //裁掉第一个，
			$where.="and ac_id in(".$ac_ids.")";
		}
		$Model=new Model();
		$query=$Model->query("select count(article_id) as total from `zjy_article` ".$where." ");
		return $query[0]['total'];
	}
	//提交文章
	public function system_article_add_post()
	{
		$Model=M("Article");
		$data=$this->post;
		if($Model->add($data)){
			return true;
		}else{
			return false;
		}
	}
    */


}